package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ContractBillInfo;
import com.sl.au.entity.User;
import com.sl.au.service.AccountsReceivableService;
import com.sl.au.service.QueryService;

@Service("AccountsReceivableService")
@Transactional
public class AccountsReceivableServiceImpl extends BaseServiceImpl implements AccountsReceivableService {

	@Autowired
	QueryService queryService;
	
	@SuppressWarnings("null")
	@Override
	public List<ContractBillInfo> getPayHistory(int start, int length, Map<String, Object> condition, User user,
			String orderName, String dir) {
		@SuppressWarnings("unchecked")
//		String zdType = condition.get("zdType").toString();
//		String hk = condition.get("hk").toString();
		HashMap<String, Object> numMap=(HashMap<String, Object>) condition.get("numMap");
		HashMap<String, Object> qMap=(HashMap<String, Object>) condition.get("qMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql1 = new StringBuilder();
		hql1.append("select a from ContractBillInfo a where (a.cContractId in (");
		StringBuilder hql;
		if(condition.get("zdType").toString().equals("应收账金额"))
			hql=queryService.buildCombinedQueryHql("ContractPaymentInitialView","contractId", condition, namedParamsMap, qMap, user);
		else{
			//System.out.println(condition.get("hk").toString());
			if(StringUtils.hasText((String)condition.get("hk")))
				hql=queryService.buildCombinedQueryHql("ContractPayPlanView","contractId", condition, namedParamsMap, qMap, user);
			else
				hql=queryService.buildCombinedQueryHql("ContractPayRegistView","contractId", condition, namedParamsMap, qMap, user);
		}
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key=="totalSum"){
				String[] a = value.toString().split(",");
				hql.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("100万以下")) hql.append(" or (e.totalSum < 1000000  )");
					if(a[i].equals("100～500万")) hql.append(" or (e.totalSum >= 1000000 and e.totalSum<5000000 )");
					if(a[i].equals("500～1000万")) hql.append(" or (e.totalSum >= 5000000 and e.totalSum<10000000 )");
					if(a[i].equals("1000万以上")) hql.append(" or (e.totalSum >= 10000000  )");
				}
				hql.append(") "); 
			}else if(key=="Contract"){
				hql.append(" and (e.supplierCode like '%" + value.toString() + "%' )");
			}else if(key=="cOrderUnit"){
				hql.append(" and (e.orderUnit like '%" + value.toString() + "%' )");
			}else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 hql.append("and (e."+key+" in ("+value.toString()+"))");
					 }
				}
			}
		}
		hql1.append(hql + ")) ");
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key=="totalSum"){
				String[] a = value.toString().split(",");
				hql1.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("100万以下")) hql1.append(" or (a.nTotalSum < 1000000  )");
					if(a[i].equals("100～500万")) hql1.append(" or (a.nTotalSum >= 1000000 and a.nTotalSum<5000000 )");
					if(a[i].equals("500～1000万")) hql1.append(" or (a.nTotalSum >= 5000000 and a.nTotalSum<10000000 )");
					if(a[i].equals("1000万以上")) hql1.append(" or (a.nTotalSum >= 10000000  )");
				}
				hql1.append(") "); 
			}else if(key=="Contract"){
				hql1.append(" and (a.cSupplierCode like '%" + value.toString() + "%' )");
			}else if(key=="cOrderUnit"){
				hql1.append(" and (a.cOrderUnit like '%" + value.toString() + "%' )");
			}else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 hql1.append("and (a."+key+" in ("+value.toString()+"))");
					 }
				}
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		//hql1.append("and (a.cSuperior like '%"+user.getSaleArea().getSuperior().toString()+"%')");
		hql1.append(" and (a.dNotionalPoolingYearAndMonth='"+condition.get("monthYear").toString()+"')");
		System.out.println(hql1.toString());
		Query query=queryService.createQueryHql(hql1.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
	}

	@Override
	public Double getSum(Map<String, Object> condition) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object> numMap=(HashMap<String, Object>) condition.get("numMap");
		HashMap<String, Object> qMap=(HashMap<String, Object>) condition.get("qMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql = new StringBuilder();
		System.out.println(condition.get("sum").toString());
		if(condition.get("sum").toString().equals("应收账金额"))
			hql=queryService.buildCombinedQueryHql("ContractPaymentInitialView","sum(e.nAmount)", condition, namedParamsMap, qMap, null);
		else{
			if(StringUtils.hasText((String)condition.get("hk")))
				hql=queryService.buildCombinedQueryHql("ContractPayPlanView","sum(e.nAmount)", condition, namedParamsMap, qMap, null);
			else
				hql=queryService.buildCombinedQueryHql("ContractPayRegistView","sum(e.nAmount)", condition, namedParamsMap, qMap, null);
		}
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if(key=="totalSum"){
				String[] a = value.toString().split(",");
				hql.append(" and ( 1<>1 ");
				for(int i=0;i<a.length;i++){
					if(a[i].equals("100万以下")) hql.append(" or (e.totalSum < 1000000  )");
					if(a[i].equals("100～500万")) hql.append(" or (e.totalSum >= 1000000 and e.totalSum<5000000 )");
					if(a[i].equals("500～1000万")) hql.append(" or (e.totalSum >= 5000000 and e.totalSum<10000000 )");
					if(a[i].equals("1000万以上")) hql.append(" or (e.totalSum >= 10000000  )");
				}
				hql.append(") "); 
			}else if(key=="Contract"){
				hql.append(" and (e.supplierCode like '%" + value.toString() + "%' )");
			}else if(key=="cOrderUnit"){
				hql.append(" and (e.orderUnit like '%" + value.toString() + "%' )");
			}else{
				if (value instanceof String){
					 if(StringUtils.hasText((String)value)){
						 hql.append("and (e."+key+" in ("+value.toString()+"))");
					 }
				}
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		if(StringUtils.hasText(dataFilter)){
			//this.contractDataFilter(hql, dataFilter, user);
		}else{
			//return null;
		}
		//hql.append(" order by " + orderName + " " + dir);
		Query query=queryService.createQueryHql(hql.toString());
		System.out.println(hql.toString());
		if (null != query && null != namedParamsMap) {
			queryService.setNamedParams(query, namedParamsMap);
		}
		Double d = (Double)query.getSingleResult();
		return d;
	}

}
